package com.kc.saf.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kc.saf.domain.CaseManagement;
import com.kc.saf.domain.SafetyAlertType;
import com.kc.saf.service.CaseManagementService;
import com.kc.saf.service.SafetyAlertTypeService;
import com.kc.saf.vo.CaseManagementVo;
import com.kc.sys.common.Constast;
import com.kc.sys.common.DataGridView;
import com.kc.sys.common.R;
import com.kc.sys.domain.User;
import com.kc.sys.util.WebUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老杜
 * @since 2019-11-07
 */
@RestController
@RequestMapping("/caseManagement")
public class CaseManagementController {

    @Autowired
    private CaseManagementService caseManagementService;
    @Autowired
    private SafetyAlertTypeService safetyAlertTypeService;
    /**
     * 查询
     */
    @RequestMapping("loadAllCaseManagement")
    public DataGridView loadAllCaseManagement(CaseManagementVo caseManagementVo){
        //获取用户session
        //User user = (User)WebUtils.getSession().getAttribute("user");
        IPage<CaseManagement> page = new Page<>(caseManagementVo.getPage(),caseManagementVo.getLimit());
        QueryWrapper<CaseManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(caseManagementVo.getSafetyId()!=null&&caseManagementVo.getSafetyId()!=0,"safety_id",caseManagementVo.getSafetyId());
        queryWrapper.like(StringUtils.isNotBlank(caseManagementVo.getCaseTitle()),"case_title",caseManagementVo.getCaseTitle());
        queryWrapper.orderByDesc("ordernum").orderByDesc("create_date");
        caseManagementService.page(page,queryWrapper);
        List<CaseManagement> caseManagements = page.getRecords();
        for (CaseManagement caseManagement : caseManagements) {
            SafetyAlertType safetyAlertType = safetyAlertTypeService.getById(caseManagement.getSafetyId());
            if(null!=safetyAlertType){
                caseManagement.setSafetyName(safetyAlertType.getTypename());
            }
        }
        return new DataGridView(page.getTotal(),page.getRecords());
    }
    //添加案例详情
    @RequestMapping("addCaseManagement")
    public R addCaseManagement(CaseManagementVo caseManagementVo){
        try {
            User user = (User)WebUtils.getSession().getAttribute("user");
            caseManagementVo.setAuthor(user.getName());
            caseManagementVo.setCreateDate(new Date());
            caseManagementService.save(caseManagementVo);
            return R.ok().message("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }
    //更新案例详情
    @RequestMapping("updateCaseManagement")
    public R updateCaseManagement(CaseManagementVo caseManagementVo){
        try {

            caseManagementService.updateById(caseManagementVo);
            return R.ok().message("更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }
    /**
     * 根据Id 删除
     * */
    @RequestMapping("deleteCaseManagement")
    public R deleteById(Integer id){
        try {
            caseManagementService.removeById(id);
            return R.ok().message("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    //加载最大的排序码
    @RequestMapping("loadCaseMaxOrderNum")
    public Map<String ,Object> loadCaseMaxOrderNum(){
        Map<String,Object> map = new HashMap<>();
        QueryWrapper<CaseManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<CaseManagement> list = caseManagementService.list(queryWrapper);
        if(list.size()>0){
            map.put("value",list.get(0).getOrdernum()+1);
        }else{
            map.put("value",1);
        }
        return map;
    }

    //根据Id查询案例汇编
    @RequestMapping("loadCaseById/{id}")
    public R loadCaseById(@PathVariable("id") Integer id){
        try {
            CaseManagement caseManagement = caseManagementService.getById(id);
            return R.ok().data("item",caseManagement);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("没有数据");
        }
    }
    //根据安全警示类型查询
    @RequestMapping("loadCaseByType/{type}")
    public R loadCaseByType(@PathVariable("type") Integer type){
        QueryWrapper<CaseManagement> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("safety_id",type);
        queryWrapper.eq("available",1);
        queryWrapper.orderByDesc("create_date");
        List<CaseManagement> list = caseManagementService.list(queryWrapper);
        return  R.ok().data("item",list);
    }
}

